package fileexplorer.view;

import fileexplorer.Configuration;
import fileexplorer.Constants;
import fileexplorer.model.FileManager;
import fileexplorer.model.Leaf;
import fileexplorer.util.Invoker;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: rejeev
 * Date: 4/29/13
 * Time: 8:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class FileTable extends JTable {
    private MainWindow mainWindow;
    private TablePopupMenu popupMenu;
    private FileManager fileManager;
    private String path;

    public FileTable(Object[][] data, Object[] columnNames, MainWindow mainWindow, FileManager fileManager){
        super(data, columnNames);
        this.mainWindow = mainWindow;
        this.fileManager = fileManager;
        initialize();
    }

    private void initialize(){
        this.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));
        this.setRowHeight(20);
        this.setFillsViewportHeight(true);
        this.setAutoCreateRowSorter(true);
        this.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        this.setShowVerticalLines(false);
        this.getTableHeader().setReorderingAllowed(false);
        popupMenu = new TablePopupMenu(this);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent mouseEvent) {
                if(mouseEvent.isPopupTrigger()) popUp(mouseEvent);
            }
            @Override
            public void mouseReleased(MouseEvent mouseEvent){
                if(mouseEvent.isPopupTrigger()) popUp(mouseEvent);
            }
            private void popUp(MouseEvent mouseEvent){
                JTable source = (JTable) mouseEvent.getSource();
                int row = source.rowAtPoint(mouseEvent.getPoint());
                int column = source.columnAtPoint(mouseEvent.getPoint());
                if(!source.isRowSelected(row)) source.changeSelection(row, column, false, false);
                popupMenu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
            }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.VK_ENTER){
                    String filePath = ((Leaf) getValueAt(getSelectedRow(), 0)).path;
                    String extension = filePath.substring(filePath.lastIndexOf('.'));
                    String program = Configuration.getDefaultProgram(extension);
                    Invoker.invoke(filePath, program);
                }
            }
        });
    }

    public void displayFiles(String path){
        File[] files = fileManager.getFiles(path);
        DefaultTableModel defaultTableModel = constructTableModel(files);
        this.setModel(defaultTableModel);
        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
        this.getColumnModel().getColumn(1).setCellRenderer(rightRenderer);
        this.getColumnModel().getColumn(1).setMaxWidth(150);
        this.getColumnModel().getColumn(2).setMaxWidth(200);
        this.getColumnModel().getColumn(1).setMinWidth(150);
        this.getColumnModel().getColumn(2).setMinWidth(200);
        this.scrollRectToVisible(this.getCellRect(0, 0, true));
    }

    private DefaultTableModel constructTableModel(File[] files){
        if(files == null) files = new File[0];
        String[] columnNames = Constants.columnNames;
        int numberOfColumns = columnNames.length;
        int numberOfFiles = files.length;
        Object[][] data = new Object[numberOfFiles+1][numberOfColumns];
        data[0] = columnNames;
        int i = 1;
        for(File file : files){
            String name = file.getName();
            //long size = file.length();
            String sizeString = new DecimalFormat("###,###").format(file.length());
            //long modified = file.lastModified();
            String modifiedTime = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss").format(new Date(file.lastModified()));
            String path = file.getAbsolutePath();
            Leaf leaf = new Leaf(name, path);
            Object[] row = {leaf, sizeString, modifiedTime};
            data[i++] = row;
        }
        DefaultTableModel defaultTableModel = new DefaultTableModel(data, columnNames);
        return defaultTableModel;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}
